// components/calendar/index.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isSelect: {
      type: Boolean,
      value: true
    },
    selectDay: {
      type: String,
      value: ''
    }
  },
  // 生命周期
  lifetimes: {
    ready() {
      this._initDate()
      setTimeout(() => {
        this.initDay()
      }, 1000)
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    week: ['日', '一', '二', '三', '四', '五', '六'],
    line: 0,
    dateArray: [],
    year: new Date().getFullYear(), // 年份
    month: new Date().getMonth(), // 月份
    day: [] // 选取的天数
  },

  /**
   * 组件的方法列表
   */
  methods: {
    initDay() {
      if(!this.properties.selectDay.length)return
      this.setData({
        day: this.properties.selectDay.split(',')
      })
      if (!this.properties.selectDay.length) return
      const month = Number(this.data.day[0].charAt(5))
      const nowMonth = new Date().getMonth() + 1
      if (month > nowMonth) {
        for (let i = nowMonth; i < month; i++) {
          this.bindyearAdd()
        }
      } else if (month < nowMonth) {
        for (let i = month; i < nowMonth; i++) {
          this.bindyearReaduce()
        }
      }
    },
    _isLeap(year) {
      if (typeof year !== 'number') {
        return
      }
      return year % 4 === 0 ?
        year % 100 !== 0 ?
        1 :
        year % 400 === 0 ?
        1 :
        0 :
        0 // 判断是闰年还是平年
    },
    // 当前月份第一天
    _firstDay(year, month) {
      let firstDay = new Date(year, month, 1)
      return firstDay
    },
    // 第一天是星期几
    _weekDay(year, month) {
      const firstDay = this._firstDay(year, month)
      return firstDay.getDay()
    },
    // 没月的天数
    _MonthArray(year) {
      return new Array(
        31,
        28 + this._isLeap(year),
        31,
        30,
        31,
        30,
        31,
        31,
        30,
        31,
        30,
        31
      )
    },
    // 当前月有多少天
    _dayNums(year, month) {
      return this._MonthArray(year)[month]
    },
    // 显示行数
    _line(year, month) {
      const line = Math.ceil(
        (this._weekDay(year, month) + this._dayNums(year, month)) / 7
      )
      this.setData({
        line
      })
      return line
    },
    // 初始化行数
    _initDate() {
      const year = this.data.year
      const month = this.data.month
      this._line(year, month)
      this._initDay(
        this._line(year, month),
        this._weekDay(year, month),
        this._dayNums(year, month)
      )
    },
    // 初始化日期
    _initDay(line, weekDay, dayNums) {
      const result = []
      for (let i = 0; i < line; i++) {
        for (let j = 0; j < 7; j++) {
          let index = 7 * i + j // 为每个表格创建索引,从0开始
          let date = index - weekDay + 1
          date <= 0 ?
            result.push('') :
            date > dayNums ?
            result.push('') :
            result.push(date)
        }
      }
      this.setData({
        dateArray: result
      })
    },
    // 减少月份
    bindyearReaduce() {
      let month = this.data.month
      let year = this.data.year
      const nowYear = new Date().getFullYear()
      const nowMonth = new Date().getMonth()
      month === 0 ?
        this.setData({
          year: --year,
          month: 11
        }) :
        this.setData({
          month: --month
        })
      this._initDay(
        this._line(this.data.year, this.data.month),
        this._weekDay(this.data.year, this.data.month),
        this._dayNums(this.data.year, this.data.month)
      )
    },
    // 增加月份
    bindyearAdd() {
      let month = this.data.month
      let year = this.data.year
      month === 11 ?
        this.setData({
          year: ++year,
          month: 0
        }) :
        this.setData({
          month: ++month
        })
      this._initDay(
        this._line(this.data.year, this.data.month),
        this._weekDay(this.data.year, this.data.month),
        this._dayNums(this.data.year, this.data.month)
      )
    },
    // 选取月份
    bindChooseDate(e) {
      if (!this.properties.isSelect) return
      const content = e._relatedInfo.anchorTargetText
      const year = this.data.year
      const month = this.data.month
      let day = this.data.day
      const date = `${year}-${month + 1}-${content}`
      if (content === '') return
      const newDay = this._include(day, date)
      this.setData({
        day: newDay
      })
      app.globalData.date = newDay
    },
    // 检测是否包含日期
    _include(arr, item) {
      const index = arr.findIndex(i => {
        return i === item
      })
      console.log(index)
      index > -1 ? arr.splice(index, 1) : arr.push(item)
      console.log(arr)
      return arr
    },
    // 全部清除
    bindClear() {
      this.setData({
        day: []
      })
      app.globalData.date = []
    },
    // 全选
    bindAll() {
      const year = new Date().getFullYear()
      const month = new Date().getMonth()
      const dayNum = this._dayNums(year, month)
      const result = []
      const day = new Array(dayNum).toString().split(',')
      day.map((item, index) => {
        result.push(`${year}-${month + 1}-${index + 1}`)
      })
      this.setData({
        day: result
      })
      app.globalData.date = result
    }
  }
})